package ide.ccf.ccproject.wizards;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

import ide.ccf.ccproject.translate.Message;
import ide.ccf.ccproject.wizards.pages.MetadataWizardPage;

import org.eclipse.cdt.internal.ui.actions.WorkbenchRunnableAdapter;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

public class MetadataWizard extends Wizard implements INewWizard 
{
	private MetadataWizardPage page;
	private ISelection selection;
	
	/**
	 * 构造器
	 */
	public MetadataWizard() 
	{
		super();
		setNeedsProgressMonitor(true);
		setWindowTitle(Message.MetadataWizard_0);
	}
	
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// TODO Auto-generated method stub
		this.selection = selection;
	}

	/**
	 * 向导完成
	 */
	@SuppressWarnings("restriction")
	@Override
	public boolean performFinish() 
	{
		IWorkspaceRunnable op = new IWorkspaceRunnable() 
		{
			public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException 
			{
				try 
				{
					createFile(monitor);
				} 
				catch (InterruptedException e) 
				{
					throw new OperationCanceledException(e.getMessage());
				}
			}
		};
		
		try 
		{
			getContainer().run(false, false, new WorkbenchRunnableAdapter(op));
		} 
		catch (Exception e) 
		{
			return false;
		}	
		return true;
	}
	
	/**
	 * 添加页面到向导
	 */
	public void addPages() 
	{
		page = new MetadataWizardPage(selection);
		addPage(page);
	}
	
	/**
	 * 创建文件
	 * @param monitor
	 * @throws InterruptedException
	 * @throws CoreException
	 */
	protected  void createFile(IProgressMonitor monitor) throws InterruptedException, CoreException
	{
		page.createFile(monitor);
	}
	
}
